class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/kyleconroy/sqlc/archive/v1.17.2.tar.gz"
  sha256 "6c8e1b66d11c4aacffad8c5e9e7586df12e5b1c24f957d4c5d78c3789da5de56"
  license "MIT"
  head "https://github.com/kyleconroy/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_ventura:  "d0b69ea618c5381f4fb070432cd6f811c35c10ccdfaf52cdb9e31bcaa8b4aec0"
    sha256 cellar: :any_skip_relocation, arm64_monterey: "c3a9612bcf20896ec074aa7c5eeff573ee5e51ad2d37fb35a4d5403078a6c5aa"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "c3db753e1793272c04b5ef7199ea0cbdb2ec299861014f80f76243af7a76ffb9"
    sha256 cellar: :any_skip_relocation, ventura:        "29cf3f2a38e175507f8ebd3046d8b35b0f215fef880342fefc27d8076c184cc7"
    sha256 cellar: :any_skip_relocation, monterey:       "0403f773b16af97c39bee8e72d92d254715a386b2af5781b24fc9a062d4562b4"
    sha256 cellar: :any_skip_relocation, big_sur:        "f659b81d1558cd5ecca81bf9a2f3f8730e2d7a4c1d6cdaec6ce836a7750f6c27"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "2073a255a0239e30b900ef06fa121fd82a6c3ff5196c9a87577356fddc6cba8a"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
